﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using IGolovkine.ShoutCastRipper.Service.Contracts;
using System.Collections.ObjectModel;
using IGolovkine.Core.Collections.Specialized;

namespace IGolovkine.ShoutCastRipper.UI.AdminPanel
{
    public class AdminPanelModel
    {
        private readonly ObservableList<StationRipperStatusItem> _stationRipperStatusItems;
        private readonly ObservableList<StationRipperEventItem> _stationRipperEventItems;
        private readonly AdminPanelStatus _status;

        public AdminPanelModel()
        {
            _stationRipperStatusItems = new ObservableList<StationRipperStatusItem>();
            _stationRipperEventItems = new ObservableList<StationRipperEventItem>();
            _status = new AdminPanelStatus();
        }

        public AdminPanelStatus Status
        {
            get { return _status; }
        }

        public ObservableList<StationRipperEventItem> EventItems
        {
            get { return _stationRipperEventItems; }
        }

        public ObservableList<StationRipperStatusItem> StatusItems
        {
            get { return _stationRipperStatusItems; }
        }

        public void UpdateStationRipperStatusItems(IEnumerable<StationRipperStatusDataContract> statusData)
        {
            _stationRipperStatusItems.RemoveAll(i => statusData.Count(t => t.Name == i.Name) == 0);
            foreach (StationRipperStatusDataContract status in statusData)
            {
                StationRipperStatusItem item = _stationRipperStatusItems.FirstOrDefault(i => i.Name == status.Name);
                if (item == null)
                {
                    item = new StationRipperStatusItem();
                    _stationRipperStatusItems.Add(item);
                }
                item.Name = status.Name;
                item.ContentTitle = status.ContentTitle;
                if (status.CurrentLength < TimeSpan.Zero)
                {
                    item.ContentLength = "-";
                }
                else
                {
                    item.ContentLength = string.Format("{0:#0}:{1:00}:{2:00}", status.CurrentLength.Hours, status.CurrentLength.Minutes, status.CurrentLength.Seconds);
                }
                item.Status = status.Status;
                item.Url = status.Url;
            }
        }

        public void UpdateStationRipperEventItems(IEnumerable<StationRipperEventDataContract> eventData)
        {
            Func<StationRipperEventDataContract, StationRipperEventItem, bool> comparer = (d, i) => d.Description == i.Description && d.SourceName == i.SourceName && d.Time == i.Time;
            _stationRipperEventItems.RemoveAll(i => eventData.Count(t=> comparer(t, i)) == 0);
            foreach (StationRipperEventDataContract evt in eventData)
            {
                StationRipperEventItem item = _stationRipperEventItems.FirstOrDefault(i => comparer(evt, i));
                if (item == null)
                {
                    item = new StationRipperEventItem();
                    _stationRipperEventItems.Insert(0, item);
                }
                item.Description = evt.Description;
                item.SourceName = evt.SourceName;
                item.Time = evt.Time;
            }
        }
    }
}
